﻿package smooth.core.canvas.liquidCanvas {
	
	import app.core.object.ImageObject;
	import app.core.action.RotatableScalable;
	import app.core.utl.ColorUtil;
	import caurina.transitions.Tweener;
	import flash.display.Stage;
	import flash.display.Shape;		
	import flash.events.Event;
	import flash.geom.Point;	      
	import flash.filters.BitmapFilterQuality;
	import flash.filters.*;
	import flash.events.*;
	import smooth.core.object.menuButtonComponent.MenuButtonChangedStateEvent;
	import smooth.core.object.menuButtonComponent.MenuButtonComponent;
	import smooth.core.service.gesture.GestureComponent;
	import flash.display.DisplayObjectContainer;
	
	
	public class LiquidCanvas extends RotatableScalable	{		
		private var clickgrabber:Shape = new Shape();	
		// START - 20081129 - LSA
		//protected var gestureComponent1:GestureComponent;
		protected var menuButton:MenuButtonComponent;
		protected var menu:Object;
		// END - 20081129 - LSA
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var initialAlpha:Number;
		
		// START - 20081129 - LSA
		//public function LiquidCanvas(posX:int, posY:int, sizeX:int, sizeY:int, color:int, alpha:Number = 0.5):void {
		public function LiquidCanvas(posX:int, posY:int, sizeX:int, sizeY:int, color:int = 0x009922, alpha:Number = 0.5):void {
		// END - 20081129 - LSA
			// Init des attributs de l'objet
			this.bringToFront = true;			
			this.noScale = false;
			this.noRotate = false;
			this.noMove = false;
			this.initialWidth = sizeX;
			this.initialHeight = sizeY;
			this.initialPosX = posX;
 			this.initialPosY = posY;
			this.initialColor = color;
			this.initialAlpha = alpha;
			// Init des visuels du composant			
			this.initGraphics();
			// Init des events du composant
			this.initEvents();	
			// Init des process du composant
			this.initProcess();
		}	
		
		public function doAnimationFadeIn():void {
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			Tweener.addTween(this, {scaleX:1, scaleY:1, time:0.5, transition:"easeInOutBack" } );	
			this.width = initialWidth;
			this.height = initialHeight;
		}
		
		private function touchDown(e:TouchEvent):void {
			var glow:GlowFilter =new GlowFilter();
			// START - 20081129 - LSA
			//glow.color = 0x009922;
			glow.color = this.initialColor;
			// END - 20081129 - LSA
			glow.alpha =1;
			glow.blurX =25;
			glow.blurY =25;
			glow.quality =BitmapFilterQuality.MEDIUM;
			clickgrabber.filters = [glow ];
			// START - 20081129 - LSA
			//this.gestureComponent1.alpha = 1;
			this.menuButton.alpha = this.menuButton.initialAlpha;
			// END - 20081129 - LSA
		}
		
		private function touchUp(e:TouchEvent):void {
			clickgrabber.filters = [];
			// START - 20081129 - LSA
			//this.gestureComponent1.alpha = 0;
			if (this.menu.visible) {
				this.menuButton.alpha = this.menuButton.initialAlpha;
			} else {				
				this.menuButton.alpha = 0;
			}
			// END - 20081129 - LSA
		}
		
		private function initGraphics():void {
			this.x = initialPosX;
			this.y = initialPosY;
			// START - 20081129 - LSA
			//clickgrabber.graphics.lineStyle(2,this.initialColor,this.initialAlpha);
			//clickgrabber.graphics.beginFill(initialColor, this.initialAlpha);
			clickgrabber.graphics.lineStyle(2,0xFFFFFF,this.initialAlpha);
			clickgrabber.graphics.beginFill(0xFFFFFF, this.initialAlpha);
			// END - 20081129 - LSA
			clickgrabber.graphics.drawRoundRect(0,0,initialWidth,initialHeight,20,20);
			clickgrabber.graphics.endFill();
			this.addChild(clickgrabber);
			// START - 20081129 - LSA
			//this.gestureComponent1 = new GestureComponent(this, this.width-40, 0, 40, 40, 0xFFFFFF, 1);
			//this.addChild(this.gestureComponent1);
			// Add menu component
			//this.menu = new LiquidCanvasMenu(this.width - 40, 40, 0x0000FF, 160);
			this.menu = new LiquidCanvasMenu(this.width-40, 40, 0xFFFFFF, 160);
			this.menu.visible = false;
			this.addChild(this.menu);
			this.menu.scaleX = 0.5;
			this.menu.scaleY = 0.5;
			// Add menu button component
			//this.menuButton = new MenuButtonComponent(this, this.width - 40, 40, 60, 60, 0x0000FF, 0.5);
			this.menuButton = new MenuButtonComponent(this, this.width - 40, 40, 60, 60, 0xFFFFFF, 0.5);
			this.addChild(this.menuButton);
			this.menuButton.addEventListener(MenuButtonChangedStateEvent.menuButtonChangedState, this.showHideMenu);
			// END - 20081129 - LSA
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_DOWN, this.touchDown);						
			this.addEventListener(TouchEvent.MOUSE_UP, this.touchUp);	
		}
		
		public function removeComponent():void {
			var parentObject1:DisplayObjectContainer = this.parent;
			parentObject1.removeChild(this);
		}
		
		private function showHideMenu(e:MenuButtonChangedStateEvent):void {
			if (e.show) {
				this.menu.alpha = 0;
				this.menu.visible = true;
				Tweener.addTween(this.menu, { alpha:1, time:1.5, transition:"easeOutBack" } );
			} else {
				this.menu.visible = false;
			}
		}
	}
}

